import axios from './axios';

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL;

// 目录配置类型定义
export interface DirectoryConfig {
  PATH: string;
  NAME: string;
  HISTORY?: string;
  ISMON?: number;
  USER?: string;
  PWD?: string;
  IP?: string;
}

export interface SystemConfig {
  ROLL?: Record<string, DirectoryConfig>;
  SYNC?: Record<string, DirectoryConfig>;
  PUB?: Record<string, DirectoryConfig>;
}

// 获取所有系统配置
export const getAllSysConf = async (): Promise<{
  success: boolean;
  data?: SystemConfig;
  message?: string;
}> => {
  try {
    const token = localStorage.getItem('token') || '';
    const response = await fetch(`${API_BASE_URL}/-/bin.sysConf.get_all_sys_conf`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'token': token
      },
      body: JSON.stringify({})
    });

    const data = await response.json();
    
    if (data.code === 200 || data.code === 0) {
      return {
        success: true,
        data: data.data || {}
      };
    } else {
      return {
        success: false,
        message: data.msg || data.message || '获取配置失败'
      };
    }
  } catch (error) {
    console.error('获取系统配置失败:', error);
    return {
      success: false,
      message: '网络请求失败'
    };
  }
};

// 更新系统配置
export const updateSysConf = async (config: {
  L1: string;
  L2: Record<string, DirectoryConfig>;
}): Promise<{
  success: boolean;
  message?: string;
}> => {
  try {
    const token = localStorage.getItem('token') || '';
    const response = await fetch(`${API_BASE_URL}/-/bin.sysConf.update_sys_conf`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'token': token
      },
      body: JSON.stringify(config)
    });

    const data = await response.json();
    
    if (data.code === 200 || data.code === 0) {
      return {
        success: true
      };
    } else {
      return {
        success: false,
        message: data.msg || data.message || '更新配置失败'
      };
    }
  } catch (error) {
    console.error('更新系统配置失败:', error);
    return {
      success: false,
      message: '网络请求失败'
    };
  }
};

// 验证路径格式
export const validatePath = (path: string): boolean => {
  return /^\/[a-zA-Z0-9\-_\/]*$/.test(path);
};

// 删除系统配置
export const deleteSysConf = async (config: {
  L1: string;
  L2: string;
}): Promise<{
  success: boolean;
  message?: string;
}> => {
  try {
    const token = localStorage.getItem('token') || '';
    const response = await fetch(`${API_BASE_URL}/-/bin.sysConf.del_sys_conf`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'token': token
      },
      body: JSON.stringify(config)
    });

    const data = await response.json();
    
    if (data.code === 200 || data.code === 0) {
      return {
        success: true
      };
    } else {
      return {
        success: false,
        message: data.msg || data.message || '删除配置失败'
      };
    }
  } catch (error) {
    console.error('删除系统配置失败:', error);
    return {
      success: false,
      message: '网络请求失败'
    };
  }
};

// 获取配置项的类型标签
export const getConfigTypeTag = (category: string): { text: string; color: string } => {
  const typeMap = {
    ROLL: { text: '归档存储', color: 'blue' },
    SYNC: { text: '同步配置', color: 'green' },
    PUB: { text: '发布配置', color: 'orange' }
  };
  return typeMap[category as keyof typeof typeMap] || { text: '未知', color: 'default' };
}; 